﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ClassLibrary.DAO;
using ClassLibrary.Data;
using ClassLibrary.Entity;

public partial class Client_UserControl_ucProcducts : System.Web.UI.UserControl
{
    TblItemsDAO ItemsDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 300000; i <= 3000000; i += 500000)
            {
                string n;
                n = string.Format("{0:0,0}", i);
                drPriceFrom.Items.Add(n.ToString());
            }
            for (int j = 500000; j <= 22000000; j += 4000000)
            {
                string n;
                n = string.Format("{0:0,0}", j);
                drPriceTo.Items.Add(n.ToString());
            }
        }
        
    }

    public void LoadData(string strName, string strCatID, string strOrderby)
    {
        try
        {
            lvItems.DataSource = ItemsDAO.GetProduct(0,strName, strCatID, 1, 0, 100, "", 1);
            lvItems.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    protected void btnSortedAsc_Click(object sender, ImageClickEventArgs e)
    {
          try
            {
               // int maxrow1 = itemDAO.GetItemCount("", "", cboSortBy.SelectedValue);
                lvItems.DataSource = ItemsDAO.GetProduct(0, "", "", 1, 0, 100, cboSortBy.SelectedValue + " " + "ASC", 1);
                lvItems.DataBind();
            }
            catch (Exception ex)
            {
                Session[Global.SESSION_ERROR] = ex;
                Response.Redirect(Global.ERROR_PAGE);
            }  
    }
    protected void btnSortDesc_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            // int maxrow1 = itemDAO.GetItemCount("", "", cboSortBy.SelectedValue);
            lvItems.DataSource = ItemsDAO.GetProduct(0, "", "", 1, 0, 100, cboSortBy.SelectedValue + " " + "DESC", 1);
            lvItems.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }  
    }
  
    protected void lvItems_PagePropertiesChanged(object sender, EventArgs e)
    {
        lvItems.DataBind();
    }
    protected void bttim_Click(object sender, EventArgs e)
    {
        try
        {
            if ((drPriceFrom.SelectedValue == "Giá từ..." && drPriceTo.SelectedValue != "Giá đến...") || (drPriceFrom.SelectedValue != "Giá từ..." && drPriceTo.SelectedValue == "Giá đến..."))
            {
                LoadData("", "", "");
            }
            else if (drPriceFrom.SelectedValue == "Giá từ..." && drPriceTo.SelectedValue == "Giá đến...")
            {
                lvItems.DataBind();
            }
            else
            {
                lvItems.DataSource = ItemsDAO.GetItemPrice(float.Parse(drPriceFrom.SelectedValue.ToString()), float.Parse(drPriceTo.SelectedValue.ToString()), 1);
                lvItems.DataBind();
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
    }
}
